{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}

<script>

$(() => {
    doAsync(
        async () => {
            let id = parseInt('{{ id }}');
            if (id > 0) {
	            let resp = await getJson(`/api/categories/${id}`);
	            return {
	    		    title: 'Edit Category',
	                category: resp
	            };
	        } else {
	            return {
                    title: 'New Category',
                    category: {}
                };
	        }
        },
    	data => initVM(data),
        err => UIkit.modal.alert(translateError(err))
    );
});

function initVM(data) {
    window.vm = new Vue({
        el: '#vm',
        data: {
        	title: data.title,
            category: data.category
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            submit: async function() {
                clearFormError();
                try {
                    await postJson('{{ action }}', this.category);
                    location.assign('/manage/article/category_list');
                } catch (err) {
                    onJsonFormError(err);
                }
            },
            cancel: function () {
                location.assign('/manage/article/category_list');
            }
        }
    });
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-tab" data-uk-tab>
            <li class="uk-disabled"><a href="#0">{{ _('Articles') }}</a></li>
            <li class="uk-active"><a href="#0">{{ _('Categories') }}</a></li>
        </ul>
        <form v-on:submit.prevent="submit" class="uk-margin uk-form uk-form-stacked">
            <legend v-text="title"></legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Name') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="category.name" name="name" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">{{ _('Tag') }}:</label>
                    <div class="controls">
                        <input v-model="category.tag" name="tag" type="text" class="uk-width-1-1" placeholder="tag">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Description') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="category.description" name="description" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="uk-form-controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> {{ _('Save') }}</button>
                        <button type="button" v-on:click="cancel" class="uk-button"><i class="uk-icon-times"></i> {{ _('Cancel') }}</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
